<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="封面" prop="coverImage">
        <single-upload v-model="dataForm.coverImage"></single-upload>
        <!-- <el-input v-model="dataForm.coverImage" placeholder="封面"></el-input> -->
      </el-form-item>
      <el-form-item label="文本描述" prop="description">
        <el-input
          v-model="dataForm.description"
          placeholder=""
          type="textarea"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder=""></el-input>
    </el-form-item> -->
      <el-form-item label="时间" prop="time">
        <div class="block">
          <el-date-picker
            v-model="dataForm.time"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00']"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="最多选择" prop="maxChoices">
        <el-input
          v-model="dataForm.maxChoices"
          placeholder="最多可选几项"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否允许匿名" prop="isAnonymous">
        <el-input
          v-model="dataForm.isAnonymous"
          placeholder="是否允许匿名 0--不允许，1--允许"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否公开投票结果" prop="isPublic">
        <el-input
          v-model="dataForm.isPublic"
          placeholder="是否公开投票结果"
        ></el-input>
      </el-form-item>
      <el-form-item label="发起者id" prop="creatorId">
        <el-input
          v-model="dataForm.creatorId"
          placeholder="发起者id"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload.vue";
export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        creatorId: "",
        title: "",
        description: "",
        coverImage: "",
        time: [],
        maxChoices: "",
        isAnonymous: "",
        isPublic: "",
      },
      dataRule: {
        creatorId: [
          { required: true, message: "发起者id不能为空", trigger: "blur" },
        ],
        title: [{ required: true, message: "不能为空", trigger: "blur" }],
        description: [{ required: true, message: "不能为空", trigger: "blur" }],
        coverImage: [
          { required: true, message: "封面不能为空", trigger: "blur" },
        ],
        time: [{ required: true, message: "不能为空", trigger: "blur" }],
        maxChoices: [
          { required: true, message: "最多选择不能为空", trigger: "blur" },
        ],
        isAnonymous: [
          {
            required: true,
            message: "是否允许匿名 0--不允许，1--允许不能为空",
            trigger: "blur",
          },
        ],
        isPublic: [
          {
            required: true,
            message: "是否公开投票结果不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/voteOnline/voteevent/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.creatorId = data.voteEvent.creatorId;
              this.dataForm.title = data.voteEvent.title;
              this.dataForm.description = data.voteEvent.description;
              this.dataForm.coverImage = data.voteEvent.coverImage;
              this.dataForm.time = [data.voteEvent.startTime, data.voteEvent.endTime];
              this.dataForm.maxChoices = data.voteEvent.maxChoices;
              this.dataForm.isAnonymous = data.voteEvent.isAnonymous;
              this.dataForm.isPublic = data.voteEvent.isPublic;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/voteOnline/voteevent/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              creatorId: this.dataForm.creatorId,
              title: this.dataForm.title,
              description: this.dataForm.description,
              coverImage: this.dataForm.coverImage,
              startTime: this.dataForm.time[0],
              endTime: this.dataForm.time[1],
              maxChoices: this.dataForm.maxChoices,
              isAnonymous: this.dataForm.isAnonymous,
              isPublic: this.dataForm.isPublic,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
